package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.bean.Excerpt;
import me.shurufa.event.EventObject;
import me.shurufa.fragments.ShareEditFragment;
import me.shurufa.bean.BookComment;
import me.shurufa.model.Digest;
import me.shurufa.utils.Constants;

/**
 * 发现页点击图书跳转页面
 *
 * @FileName: me.shurufa.activities.AboutBookActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-22 09:59
 */
public class ShareEditActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.right_image)
    ImageView rightImage;

    @Bind(R.id.toolbar_title)
    TextView titleText;

    private String mBookname;
    private Excerpt mExcerpt;
    private BookComment mComment;
    private ShareEditFragment mShareEditFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_share_edit;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
        initData();
    }

    public static void initArguments(Intent in, String bookName, Excerpt excerpt, BookComment comment) {
        in.putExtra(Constants.ARG_BOOK_TITLE, bookName);
        in.putExtra(Constants.ARG_SHARE_BEAN_DIGEST, excerpt);
        in.putExtra(Constants.ARG_SHARE_BEAN_COMMENT, comment);
    }

    private void parseArguments() {
        mBookname = getIntent().getStringExtra(Constants.ARG_BOOK_TITLE);
        this.mExcerpt = (Excerpt) getIntent().getSerializableExtra(Constants.ARG_SHARE_BEAN_DIGEST);
        this.mComment = (BookComment) getIntent().getSerializableExtra(Constants.ARG_SHARE_BEAN_COMMENT);
    }

    private void initData() {
        mShareEditFragment = ShareEditFragment.newInstance(mBookname, mExcerpt, mComment);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, mShareEditFragment);
        transaction.commit();
    }

    private void initUI() {
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setOnClickListener(this);
        rightImage.setImageResource(R.drawable.ic_share_new_right);
        titleText.setText(mBookname);
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.right_image:
                mShareEditFragment.openSharePopup(v);
                break;
        }
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (intent.getAction().equals(Constants.ACTION_BOOKINFO_ADD_COMMENT)){
        }else if (intent.getAction().equals(Constants.ACTION_ADD_DIGEST_COMPLETE)){
            finish();
        }
    }

    public void onEventMainThread(EventObject object){
        if (object.getEventAction().equals(Constants.ACTION_WRITE_NOTE)){
            Digest digest = object.getDigest();
        }
    }
}
